<template>
  <a-modal
    title="采购清单"
    :width="900"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="供应商">
              <a-select
                show-search
                :filter-option="filterOption"
                placeholder="请选择供应商"
                v-decorator="['supplierId',{ rules: [{ required: true, message: '请选择供应商' }] }]" >
                <a-select-option
                  v-for="i in suppliers"
                  :key="i.supplierId"
                  :values="i.supplierId">
                  {{ i.companyName }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <div style="top: 0px;left: 370px;position: absolute;">
              <div title="刷新供应商" @click="() => { $emit('resetSuppliers') }" >
                <a-icon style="cursor: pointer" type="undo" />
              </div>
              <div title="新增供应商" @click="addSupplier" >
                <a-icon style="cursor: pointer" type="plus" />
              </div>
            </div>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物品名称">
              <a-input
                placeholder="请输入物品名称"
                v-decorator="['goodsName' ,{ rules: [{ required: true, message: '请输入物品名称' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="品牌">
              <a-input
                placeholder="请输入品牌"
                v-decorator="['brand', { rules: [{ required: true, message: '请输入品牌' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="参数">
              <a-input
                placeholder="请输入参数"
                v-decorator="['options']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="单位">
              <a-input
                placeholder="请输入单位（台、千克等）"
                v-decorator="['unit', { rules: [{ required: true, message: '请输入单位' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数量">
              <a-input
                placeholder="请输入物品数量"
                v-decorator="['count',{ rules: [{ required: true, message: '请输入物品数量' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="单价">
              <a-input
                placeholder="请入物品单价。单位 元"
                v-decorator="['price',{ rules: [{ required: true, message: '请入物品单价。单位 元' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="清单类型">
              <a-select
                v-decorator="['type']"
                :disabled="true"
              >
                <a-select-option :value="0">经营管理采购</a-select-option>
                <a-select-option :value="1">项目合同</a-select-option>
                <a-select-option :value="2">计划采购</a-select-option>
                <a-select-option :value="3">实际采购</a-select-option>
                <a-select-option :value="4">交付清单</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注">
              <a-input
                placeholder="请输入备注"
                v-decorator="['note']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['id', 'staffName', 'projectDuty', 'note']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    suppliers: {
      type: Array,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    addSupplier () {
      const _url = window.location.href.split('#')
      window.open(`${_url[0]}#/supplier/supplierList`)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>
